<?php
/**
 * Created by PhpStorm.
 * User: costa92
 * Date: 2016/10/26
 * Time: 下午5:15
 */

namespace App\Libraries\classs;

use App\Models\PrizeRank;
use App\Models\User\Prize\Logs as Logs;
use App\Models\Prize;
class Prizes
{

    static $instance;
    public static function  getInstance(){
        if( self::$instance==null ){
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function getPrizeRand($_uid){
        $where="seven >= 0";
        if($this->getPrizeLogCount($_uid)){
            $where="seven > 0";
        }
        $prizerank  = PrizeRank::whereRaw($where)->get(array('id','name','count','chance'));
        if($prizerank->toArray()){
            foreach ($prizerank->toArray() as $key => $val) {
                if($val['count'] > 0){
                    $arr[$val['id']] = $val['chance'];
                }
            }
            $rid = get_rand($arr); //根据概率获取奖项id

            $prize_info = PrizeRank::find($rid);
            $prize_info->seven;
            $prize=$this->getPrize($_uid,$rid,$prize_info->prize_chance);
            if($prize['success']==true){
                return array('success'=>true,'result'=>setRand($prize_info->seven),'r_id'=>$rid,'data'=>$prize['data']);
            }
            return array('success'=>false,'msg'=>$prize['msg']);
        }
        return array('success'=>false,'msg'=>"暂时没有数据!请稍后访问!");
    }

    public function getPrizeLogCount($_uid){
        $count = Logs::where('x_uid','=',$_uid)->where('created_at','>=',date('Y-m-d'))->where('status','=',-1)->count();
        if($count == 2){
            return true;
        }
        return setPrizeRand();
    }

    public function getPrize($uid,$rid,$chance_status){
        if($rid && $uid){
             if($chance_status == 0){
                 $prize = Prize::where('rank_id','=',$rid)->first();
             }elseif($chance_status == 1) {
                 $prize_list = Prize::where('rank_id', '=', $rid)->get();
                 $prize_list = $prize_list->toArray();
                 if (!$prize_list) {
                     return array('success'=>false,'msg'=>"暂时没有数据!请稍后访问1");
                 }
                 foreach ($prize_list as $key => $value) {
                     if($value['count']>0){
                         $arr[$value['id']] = $value['chance'];
                     }
                 }
                 $pid = get_rand($arr); //根据概率获取奖项id
                 $prize = Prize::find($pid);
             }
             if(!$prize){
                 return array('success'=>false,'msg'=>"暂时没有数据!请稍后访问2");
             }
             $order = time().rand(1000,9999);
            $rand_data =array(
                 'order'=>$order,
                 'x_uid'=>$uid,
                 'rank_id'=>$rid,
                 'prize_id'=>$prize->id,
                 'status'=>1,
             );

            $rank = PrizeRank::find($rid);
            if(!$rank){
                return array('success'=>false,'msg'=>"暂时没有数据!请稍后访问3");
            }

            if($rank->seven == 0){
                $rand_data['status']=-1;
                $rank->name="真遗憾！就差那么一点点，下次继续努力";
            }else{
                $rank->count = $rank->count-1;
                $rank->update();
                $rank->name="恭喜您抽中{$rank->name}请到个人中心查看";
            }
            if(Logs::create($rand_data)){
                $prize->count = $prize->count-1;
                $prize->update();
                return array('success'=>true,'data'=>$rank);
            }
        }
      return array('success'=>false,'msg'=>"请求错误!请重新请求");
    }
}